package com.cuz.hot100;

public class Q36ValidSudoku {

    public static boolean isValidSudoku1(char[][] board) {
        if (board == null) {
            return true;
        }
        int rowMax = board.length;
        int colMax = board[0].length;
        int[][] rowMemory = new int[rowMax][9];
        int[][] colMemory = new int[colMax][9];
        int[][][] nightMemory = new int[board.length / 3][board.length / 3][9];
        for (int row = 0; row < rowMax; row++) {
            for (int col = 0; col < colMax; col++) {
                if (board[row][col] == '.') {
                    continue;
                }
                int value = board[row][col] - '1';
                if (rowMemory[row][value] != 0) {
                    return false;
                } else {
                    rowMemory[row][value] = 1;
                }
                if (colMemory[col][value] != 0) {
                    return false;
                } else {
                    colMemory[col][value] = 1;
                }
                if (nightMemory[row / 3][col / 3][value] != 0) {
                    return false;
                } else {
                    nightMemory[row / 3][col / 3][value] = 1;
                }
            }
        }
        return true;
    }
}
